package com.whx.managea.service.impl;

import com.whx.managea.dao.CityDAO;
import com.whx.managea.dao.StudentDAO;
import com.whx.managea.dao.StudentTagDAO;
import com.whx.managea.entity.City;
import com.whx.managea.entity.Student;
import com.whx.managea.entity.StudentTag;
import com.whx.managea.service.StudentService;
import com.whx.managea.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDAO studentDAO;
    @Autowired
    private StudentTagDAO studentTagDAO;
    @Autowired
    private CityDAO cityDAO;

    @Override
    public List<Student> findAll(Integer page,Integer rows,String searchCol,String searchValue) {
        Integer start=(page-1)*rows;
        //把用户输入的条件作用参数传递给dao层进行条件拆线呢
        return studentDAO.findAll(start,rows,searchCol,searchValue);
    }

    @Override
    public Integer counts(String searchCol, String searchValue) {
        return studentDAO.counts(searchCol, searchValue);
    }

    //处理保存学生信息的业务逻辑方法
    @Override
    public void save(Student student,String[] tagIds) {
        //获取对应学生的年龄，星座，生肖
        //根据生日计算年龄
        int age = DateUtil.getAge(student.getBir());
        student.setAge(age);
        //计算生肖
        //Integer.valueOf(new SimpleDateFormat("yyyy").format(student.getBir()))
        String attr = DateUtil.getYear(Integer.valueOf(new SimpleDateFormat("yyyy").format(student.getBir())));
        student.setAttr(attr);
        //计算星座
        String starts = DateUtil.getConstellation(student.getBir());
        student.setStarts(starts);
        studentDAO.save(student);

        //获取对应的用户选中的标签信息
        for (String tagid:tagIds){
            //如果要想添加对应学生的标签信息， 学生id student.getId() 标签id
            StudentTag studentTag = new StudentTag();
            studentTag.setStudentid(student.getId());
            studentTag.setTagid(tagid);
            studentTagDAO.save(studentTag);
        }
        //在添加学生信息后，需要更新对应的城市的就业人数
        String cityid = student.getCityid();
        //1.获取城市对象
        City city = cityDAO.findById(cityid);
        //2.人数+1
        city.setNumbers(city.getNumbers()+1);
        //3.更新城市对象
        cityDAO.update(city);

    }
}
